package com.blog.controller;

import com.blog.entity.Blog;
import com.blog.entity.PageBean;
import com.blog.service.BlogService;
import com.blog.util.PageUtil;
import com.blog.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {
    @Resource
    private BlogService blogService;

    @RequestMapping("/index")
    public ModelAndView index(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "typeId", required = false) String typeId, @RequestParam(value = "releaseDateStr", required = false) String releaseDateStr, HttpServletRequest request) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.addObject("pageTitle", "我的博客");//设置网页标题
        mav.addObject("mainPage", "foreground/blog/list.jsp");//设置主要显示的网页

        if (StringUtil.isEmpty(page)) {
            page = "1";
        }

        PageBean pageBean = new PageBean(Integer.parseInt(page), 10);//设置分页

        Map<String, Object> map = new HashMap();
        map.put("start", pageBean.getStart());//传入参数
        map.put("size", pageBean.getPageSize());
        map.put("typeId", typeId);
        map.put("releaseDateStr", releaseDateStr);

        List<Blog> blogList = blogService.list(map);//执行查询
        mav.addObject("blogList", blogList);//将获得的博客列表添加进模型视图

        /*for (Blog blog : blogList) {
            List<String> imagesList = blog.getImagesList();
            String blogInfo = blog.getContent();
            Document doc = Jsoup.parse(blogInfo);
            Elements jpgs = doc.select("img[src$=.jpg]");
            for (int i = 0; i < jpgs.size(); i++) {
                Element jpg = (Element) jpgs.get(i);
                imagesList.add(jpg.toString());
                if (i == 2) {
                    break;
                }
            }
        }*/

        StringBuffer param = new StringBuffer();

        if (StringUtil.isNotEmpty(typeId)) {
            param.append("typeId=" + typeId + "&");
        }
        if (StringUtil.isNotEmpty(releaseDateStr)) {
            param.append("releaseDateStr=" + releaseDateStr + "&");
        }

        //添加翻页功能
        String PageCode=PageUtil.genPagination(request.getContextPath() + "/index.html", blogService.getTotal(map), Integer.parseInt(page), 10, param.toString());

        mav.addObject("pageCode", PageCode);
        mav.setViewName("index");//设置要跳转的视图名

        return mav;
    }

    @RequestMapping({"/download"})
    public ModelAndView download()
            throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("mainPage", "foreground/system/download.jsp");
        mav.addObject("pageTitle", "本系统由孙炳锐制作");
        mav.setViewName("index");
        return mav;
    }
}

